package com.kins.executer.ruledata.liteflow.service.impl;

import com.google.common.collect.Lists;
import com.kins.executer.constants.Constant;
import com.kins.executer.utils.ParamLogUtils;
import com.kins.executer.vo.MappingParamVo;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Description: 
 *@Author: 申科
 *@Date:2025/11/12 20:45
 */

public class LiteFlowBusinessServiceImpl implements DataBusinessService{
    @Resource
    private DataEngineService dataEngineService;

    @Autowired
    private SoFlowReportLogService soFlowReportLogService;

    @Override
    public Map<String, Object> engineService(String path, String method, String version, Map<String, Object> businessInput)throws Exception {
        long start = System.currentTimeMillis();
        Map<String, Object> sysMap = new HashMap<>();
        Map<String, Object> output = null;
        Map<String, Object> reMap = null;
        try{
            if(StringUtils.isEmpty(version)){
                output = dataEngineService.invokeRule(path,method,businessInput);//正式执行入口
            }else{
                output = dataEngineService.invokeRule(path,method,version,businessInput);
            }
            //成功
            sysMap.put("isSuccess","1");
            MappingParamVo vo = MappingParamVo.getData(output);
            vo.setNodeId(Constant.MAINFLOW_NODEID);
            vo.setInterfaceId(vo.getServiceArrangeId());
            //获取最终结果
            reMap = (Map<String, Object>) ParamLogUtils.getLogMap(output,ParamLogUtils.getLogInterfaceKey(vo),BaseParamEnum.OUT_PARAM.getType());
            //塞报告书签值
            addReportData(reMap,output,businessInput);
        }catch (Exception e){
            sysMap.put("isSuccess","0");
            sysMap.put("exceptionType","4");
            sysMap.put("exception",IOUtils.getStockTraceMsg(e));
        }finally {
            sysMap.put("invokeTimes",System.currentTimeMillis()-start);
            if(reMap == null){
                reMap = new HashMap<>();
            }
            reMap.put("System",sysMap);
            Long startTime = reMap.get(Constant.START_DATE_KEY)==null? start:(Long) reMap.get(Constant.START_DATE_KEY);
            Long endTime = reMap.get(Constant.END_DATE_KEY)==null? System.currentTimeMillis():(Long) reMap.get(Constant.END_DATE_KEY);
            reMap.remove(Constant.START_DATE_KEY);
            reMap.remove(Constant.RIGHTING_STACK);
            reMap.put(Constant.START_DATE_KEY,endTime);
            boolean isSt = !StringUtils.isEmpty(version);
            if(businessInput!=null){
                //处理日志
            }
        }
        return reMap;
    }

    public void addReportData(Map<String, Object> reMap,Map<String, Object> output,Map<String, Object> businessInput){
        HashMap<String, Object> resultMap = (HashMap<String, Object>) output.get(Constant.RESULT_PARAM);
        List<HashMap<String, Object>> reportData = Lists.newArrayList();
        List<SoFlowAskChunksLog> soFlowAskChunksLogList = Lists.newArrayList();
        String runFlowId = output.get(Constant.RUNFLOW_ID).toString();
        Object edocTaskObj = businessInput.get("edocTaskId");
        String edocTaskId = null;
        if(edocTaskObj != null){
            edocTaskId = edocTaskObj.toString();
        }
        ServiceInfoVo serviceInfoVo = (ServiceInfoVo) output.get("serviceArrangeInfo");
        if(resultMap != null && resultMap.size() > 0){
            for(Map.Entry<String, Object> entry : resultMap.entrySet()){
                String key = entry.getKey();
                if(key.contains("@|@reportProcessing")){
                    HashMap<String, Object> nodeMap = new HashMap<>();
                    for(Map.Entry<String, Object> nodeOutData : outMap.entrySet()){
                        String outKey = nodeOutData.getKey();
                        if(outKey.endsWith(Constant.BOOKMARKS)){
                            String[] templateKeys = outKey.split("_");
                            reportMap.put("templaateId",templateKeys[0]);
                            reportMap.put("bookMarks",nodeOutData.getValue());
                            reMap.put(outKey,nodeOutData.getValue());
                        }
                        if(outKey.endsWith(Constant.BOOKMARKS_DEFAULT_VALUE)){
                            reportMap.put("bookMarksDefaultValue",nodeOutData.getValue());
                            reMap.put(outKey,nodeOutData.getValue());
                        }
                        if(outKey.endsWith(Constant.BOOKMARKS_CHUNKS)){
                            reportMap.put("bookMarksChunks",convertChunks(soFlowAskChunksLogList,
                                    nodeOutData.getValue(),
                                    runFlowId,
                                    serviceInfoVo.getServiceArrangeId(),
                                    serviceInfoVo.getServiceArrangeVersion(),
                                    edocTaskId));
                            reMap.put(outKey,nodeOutData.getValue());
                        }
                    }
                    if(reportMap.size() >0){
                        reportData.add(reportMap);
                    }
                }
            }
        }
    }

    if()
}
